package com.imooc.controller;

import com.imooc.common.constants.ResultCodeConstants;
import com.imooc.common.result.ResponseData;
import com.imooc.common.result.ResponseUtils;
import com.imooc.dto.address.*;
import com.imooc.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: AddressController
 * @Description:
 * @author: limingxing
 * @Date: 2019-12-25 10:39
 */
@RestController
@RequestMapping("address")
@Api(value = "地址相关", tags = {"地址相关的api接口"})
public class AddressController {

    @Autowired
    private IAddressService addressService;

    @PostMapping("/add")
    @ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
    public ResponseData add(@RequestBody AddAddressRequest request) {
        AddAddressResponse response = addressService.createAddress(request);
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success();
        }
        return ResponseUtils.success(response.getMsg());
    }

    @ApiOperation(value = "用户删除地址", notes = "用户删除地址", httpMethod = "POST")
    @PostMapping("/delete")
    public ResponseData delete(@RequestParam String userId,@RequestParam String addressId) {
        DeleteAddressRequest request = new DeleteAddressRequest();
        request.setAddressId(addressId);
        request.setUserId(userId);
        DeleteAddressResponse response = addressService.deleteAddress(request);
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success();
        }
        return ResponseUtils.success(response.getMsg());
    }

    @ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址", httpMethod = "POST")
    @PostMapping("/setDefalut")
    public ResponseData setDefalut( @RequestParam String userId, @RequestParam String addressId) {
        UpdateAddressDefaultRequest request = new UpdateAddressDefaultRequest();
        request.setAddressId(addressId);
        request.setUserId(userId);
        UpdateAddressResponse response = addressService.updateAddressToBeDefault(request);
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success();
        }
        return ResponseUtils.success(response.getMsg());
    }

    @PostMapping("/update")
    @ApiOperation(value = "用户修改地址", notes = "用户修改地址", httpMethod = "POST")
    public ResponseData update(@RequestBody UpdateAddressRequest request) {
        UpdateAddressResponse response = addressService.updateAddress(request);
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success();
        }
        return ResponseUtils.success(response.getMsg());
    }


    @PostMapping("/list")
    @ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")
    public ResponseData list( @RequestParam String userId) {
        AddressListRequest request = new AddressListRequest();
        request.setUserId(userId);
        AddressListResponse response = addressService.getAddressList(request);
        if(response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())){
            return ResponseUtils.success(response.getAddressDtoList());
        }
        return ResponseUtils.success(response.getMsg());
    }



}
